using UnityEngine;
using UnityEngine.EventSystems;

namespace WorkFlow.Tools.UI
{
    [ExecuteAlways][RequireComponent(typeof(UIBehaviourDriver))]
    [AddComponentMenu("WorkFlow/UGUI/UIBehaviour/WidthPadding")]
    public class WidthPadding  : MonoBehaviour,IUIBehaviourDriverElement
    {
        [SerializeField] private int order;
        [SerializeField]RectTransform[] rectTransforms;
        [SerializeField] private float widthPadding;
        bool IUIBehaviourDriverElement.ElementEnabled => enabled;
        int IUIBehaviourDriverElement.Order => order;
        void IUIBehaviourDriverElement.OnRectTransformDimensionsChange()
        {
            Apply();
        }

        void Apply()
        {
#if UNITY_EDITOR
            _isValidate = false;
#endif
            if(transform is RectTransform rectTransform)
            {
                if (rectTransforms == null || rectTransforms.Length == 0)
                {
                    return;
                }

                foreach (var item in rectTransforms)
                {
                    if (item != null)
                    {
                        item.sizeDelta = new Vector2(rectTransform.sizeDelta.x+ widthPadding, item.sizeDelta.y );
                    }
                }
            }
        }
        
#if UNITY_EDITOR
        private bool _isValidate;
        bool IUIBehaviourDriverElement.IsValidated => _isValidate;
        void OnValidate()
        {
            _isValidate = true;
        }
#endif
    }

}